1923A - Moving Chips - CodeForces Solution


greedy greedy greedy greedy implementation

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,k=0,count=0,l=0;
        cin>>n;
        int a[n];
        for(int x=0;x<n;x++){
            cin>>a[x];
        }
        for (int m = 0; m <n; m++){
            if(a[m]==1){
                k++;
            }
        }
        for (int p = (n-1); p>=0; p--){
            if(a[p]==1){
                l=p;
                break;
            }
            continue;
        }
        if(k>1){
            for (int i = 0; i < n; i++){
                if(a[i]==1){
                    for (int j = (i+1); j < l; j++){
                        if(a[j]!=a[i]){
                            swap(a[j],a[i]);
                            count++;
                            break;
                        }
                        continue;
                    }
                }
                continue;
            }      
        }
        else{
            count=0;
        }
        cout<<count<<endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal